
importPackage(com.blocks.blocks);
importPackage(com.blocks);
importPackage(com.blocks.model);
importPackage(com.jme3.math);
importPackage(java.lang);
importPackage(com.blocks.script);
importPackage(com.jme3.math);
importPackage(com.jme3.collision);
importPackage(com.jme3.scene.debug);
importPackage(com.jme3.scene);
importPackage(com.jme3.material);
importPackage(com.blocks.controller);
importPackage(com.blocks.gui);

var block = new Object();
var counter = 0;

var direction = 0;
var rayHit = false;
var mark;
block.setup = function(name) {
	thisBlock.setName("TriggerLevelFail");
	thisBlock.setTextureRow(1.0);
	thisBlock.setTextureColumn(8.0);
	thisBlock.resetTexture();
	thisBlock.setCustomFrequency(true);
	thisBlock.setBlockListGroup(BlockListGroup.GAME);
	

}
block.loop = function(name, tpf) {
	var  found = false;
	if (thisBlock.isPowered()) {
		var results = new CollisionResults();
		var  bv = thisBlock.getWorldBound();
		var masterKeySet = ScriptBlocksApplication.getInstance().getMasterBlockMap().keySet();
		var masterKeyIterator = masterKeySet.iterator();
		while(masterKeyIterator.hasNext()){
			masterId =  masterKeyIterator.next();
			if (masterId != thisBlock.getMasterId()) {
				var masterBlock = ScriptBlocksApplication
						.getInstance().getMasterBlock(masterId);
				masterBlock.collideWith(bv, results);
				if (results.size() > 0) {
					itr = results.iterator();
					while (itr.hasNext()) {
						collisionResult = itr.next();
						if (collisionResult.getGeometry() != null) {
							var hitNode = collisionResult.getGeometry()
									.getParent();
							var distance = collisionResult.getDistance();
							if (distance <= 2.0 && hitNode != null) {

								if (hitNode != null && ScriptUtils.isBlock(hitNode)) {
									var focusBlock =  hitNode;
									if (focusBlock.getMasterId().compareTo(
											thisBlock.getMasterId()) != 0) {
										
										thisBlock.getMaterial().setColor(
												"Color", ColorRGBA.Pink);
										block.trigger();
										found = true;
										break;
									}
								}

							}

						}
					}
				}
			}
		}
	}
	if(!thisBlock.isPowered()||!found){
		thisBlock.getMaterial().setColor("Color",
				ColorRGBA.Gray);
	}

}

block.trigger = function(){
	ScriptBlocksApplication.getInstance().addBlockEvent(BlockEvent.LEVEL_FAILED);
}
block.scriptExport = function() {
	return "";
}


